package servlets;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.DateBean;
import beans.FormBean;

public class FormControl extends HttpServlet{
	private static final long serialVersionUID = 1L;
	
	private DateBean db;
	
	public FormControl(){
		System.out.println("FormControl()");
	}
	public void init(){
		System.out.println("FormControl.init()");
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		System.out.println("FormControl.doGet()");
		db = new DateBean();
		request.setAttribute("date", db);
		RequestDispatcher dispatcher = request.getRequestDispatcher("/Form.jsp");
	    dispatcher.forward(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		System.out.println("FormControl.doPost()");
		RequestDispatcher dispatcher;
		FormBean fb = new FormBean();
		fb.populate( request );
		if(fb.validate())
		{
			request.setAttribute( "form", fb );
			dispatcher = request.getRequestDispatcher("/confirm");
		}else{
			db = new DateBean();
			request.setAttribute("date", db);
			dispatcher = request.getRequestDispatcher("/Form.jsp");
		}
	    dispatcher.forward(request, response);
	}
	public void destroy(){
		System.out.println("FormControl.destroy()");
	}
}
